#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"

cmd-tags-create() {
  declare desc="creates an images tag for app via command line"
  declare cmd="tags:create"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" IMAGE_TAG="$2"

  [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
  verify_app_name "$APP"

  local IMAGE_REPO=$(get_app_image_repo "$APP")
  local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
  plugn trigger scheduler-tags-create "$DOKKU_SCHEDULER" "$APP" "$IMAGE_REPO:latest" "$IMAGE_REPO:$IMAGE_TAG"

  dokku_log_info2_quiet "Added $IMAGE_TAG tag to $IMAGE_REPO"
  plugn trigger tags-create "$APP" "$IMAGE_TAG"
}

cmd-tags-create "$@"
